package paging

type Page struct {
	Page      int
	PageSize  int
	TotalPage int
	Count     int
	First     bool
	Last      bool
	List      interface{}
}

//分页计算
func Calculation(p int, ps int) (page int, skip int, limit int) {
	if p < 1 {
		p = 1
	}
	if ps < 0 {
		ps = 20
	}
	if ps > 1000 {
		ps = 1000
	}
	page = p
	skip = (page - 1) * ps
	limit = ps
	return
}

//分页数据组装
func PageUtil(page int, pageSize int, lastPage bool, list interface{}) Page {
	totalPage := page
	if !lastPage {
		totalPage ++
	}
	return Page{
		Page:      page,
		PageSize:  pageSize,
		TotalPage: totalPage,
		First:     page == 1,
		Last:      lastPage,
		List:      list,
	}
}

//分页数据组装
func PageUtilWithCount(count int, page int, pageSize int, list interface{}) Page {
	tp := count / pageSize
	if count%pageSize > 0 {
		tp = count/pageSize + 1
	}
	return Page{
		Page:      page,
		PageSize:  pageSize,
		TotalPage: tp,
		Count:     count,
		First:     page == 1,
		Last:      page == tp,
		List:      list,
	}
}
